﻿#region 文件信息

/*----------------------------------------------------------------
//
// 文件名称：
// 文件功能描述：
// 设计要求：
//
// 文 件 名：    GeneralTreeItemsManager
// 创建者：      杨程
// 创建日期：	    2022/11/21 16:46:16

//----------------------------------------------------------------*/

#endregion


namespace Vampirewal.Core.GeneralTree;

/// <summary>
///
/// </summary>
public class GeneralTreeItemsManager<TNode> where TNode : class, ITreeNode
{
    /// <summary>
    /// 构建树
    /// </summary>
    /// <param name="nodes">树列表数据</param>
    public static List<GeneralTreeContainer<TNode>> BuildTree(IEnumerable<TNode> nodes)
    {
        //存树结构
        List<GeneralTreeContainer<TNode>> treeNodes = new List<GeneralTreeContainer<TNode>>();
        var treeContainerList = nodes.AsParallel()
                                                                          .Select(t => new GeneralTreeContainer<TNode>(t))
                                                                          .ToDictionary(t => t.TreeNode.GetCurrentNodeMark());

        foreach (var item in treeContainerList)
        {
            if (treeContainerList.ContainsKey(item.Value.TreeNode.GetParentNodeMark()))
            {
                item.Value.ParentNode = treeContainerList[item.Value.TreeNode.GetParentNodeMark()];
                treeContainerList[item.Value.TreeNode.GetParentNodeMark()].SubItems = treeContainerList[item.Value.TreeNode.GetParentNodeMark()].SubItems ??
                    new ObservableCollection<GeneralTreeContainer<TNode>>();
                treeContainerList[item.Value.TreeNode.GetParentNodeMark()].SubItems.Add(item.Value);
            }
            else
            {
                treeNodes.Add(item.Value);
            }
        }
        return treeNodes;
    }

    /// <summary>
    /// 仅用容器包裹
    /// </summary>
    /// <param name="nodes"></param>
    /// <returns></returns>
    public static List<GeneralTreeContainer<TNode>> BuildContainer(IEnumerable<TNode> nodes)
    {
        return nodes.AsParallel().Select(t => new GeneralTreeContainer<TNode>(t)).ToList();
    }

    /// <summary>
    /// 获取传入集合中的顶节点
    /// </summary>
    /// <param name="nodes">节点数据</param>
    /// <returns></returns>
    public static TNode GetRootNode(IEnumerable<TNode> nodes)
    {
        if (nodes != null && nodes.Count() > 0)
        {
            TNode tmpNode = nodes.First();
            TNode rootNode = tmpNode;
            while (true)
            {
                tmpNode = nodes.FirstOrDefault(t => t.IsSubNode(tmpNode));
                if (tmpNode == null)
                    break;
                else
                    rootNode = tmpNode;
            }
            return rootNode;
        }
        return null;
    }

    /// <summary>
    /// 获取最子级节点
    /// </summary>
    /// <param name="nodes"></param>
    /// <returns></returns>
    public static IEnumerable<TNode> GetLeafNode(IEnumerable<TNode> nodes)
    {
        List<TNode> results = new List<TNode>();
        if (nodes != null && nodes.Count() > 0)
        {
            foreach (var node in nodes)
            {
                var tmp = nodes.Where(p => p.IsParentNode(node));
                if (tmp.Count() == 0) results.Add(node);
            }
            return results;
        }
        return null;
    }
}